/**
 * Copyright (C), 2021-2038, vantai.com Inc FileName: ControllerAspect Author: viyo Date: 2021/4/16 16:25 Description:
 * controller日志切面 History: <author> <time> <version> <desc> viyo 2021/4/16 16:25 v1.0.0 controller日志切面
 */
package com.ly.demo.aspect;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * 〈一句话功能简述〉<br>
 * 〈controller日志切面〉
 *
 * @author viyo 2021/4/16 16:25
 * @since 1.0.0
 */
@Component
@Aspect
@Slf4j
public class ControllerAspect {

    @Pointcut("execution(* com.ly.demo..*.*(..))")
    public void executeController() {
    }

    @Around("executeController()")
    public Object around(ProceedingJoinPoint pointCut) throws Throwable {
        long beginTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature)pointCut.getSignature();
        String className = pointCut.getTarget().getClass().getSimpleName();
        String methodName = signature.getName();
        Object[] args = pointCut.getArgs();

        String params = JSON.toJSONString(args);
        StringBuffer buffer = new StringBuffer(className);
        buffer.append(" ").append(methodName).append(" start ").append(" params: ").append(params);
        log.info(buffer.toString());
        Object object = pointCut.proceed();
        String result = JSON.toJSONString(object);
        long time = System.currentTimeMillis() - beginTime;
        StringBuffer b = new StringBuffer(className);
        b.append(" ").append(methodName).append("  end  result: ").append(result).append(" cost: ").append(time)
            .append("ms");
        log.info(b.toString());
        return object;
    }

}
